# -*- coding = utf-8 -*-
# @Time : 2022/10/17 15:27
# @Author :Henry L
# @File : w_request.py
# @Software : PyCharm
import datetime
import logging
import os
import OrganizationTypeSpider
from Utils import Unil
import csv
import setting
import IP_Interval

def stop():
    a = ''
    while a != 'over':
        a = input("请输入over继续查询:")


class TypeRunSpider():
    def __init__(self, ntype, az, sname, log_file_num):
        current_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.proxymanager = IP_Interval.ProxyManager(ntype)
        self.doctable = setting.DOC_TABLE
        self.util = Unil()
        self.ntype = ntype
        self.az = az
        self.sname = sname
        self.escape = {}
        self.fileName = "Test.csv"
        self.logger = logging.getLogger(f'{az}_Health_Commission_Log_{current_time}')
        self.logger.setLevel(logging.DEBUG)
        handler = logging.FileHandler("log/{}_Health_Commission_Log_{}.log".format(az, current_time))
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  # 时间生成
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
        self.col_names = ['id',
                          'name',
                          'wjw_province',
                          'approval_company',
                          'register',
                          'address',
                          'department',
                          'grade',
                          'legal_person',
                          'manager',
                          'hos_nature',
                          'hos_nature_',
                          'back_date',
                          'service_part',
                          'licence_date_begin',
                          "licence_date_end",
                          ]
        # 添加计数器
        self.total_proxy_used = 0
        self.total_calls = 0
        self.success_calls = 0
        self.failure_calls = 0

    def run(self):
        global start_time
        self.if_file_exist(self.fileName, self.col_names)
        escape_list = ['人民医院', '儿童医院', '大学附属医院', '大学附属', '大学附属医院']
        hos_list = self.util.queryDataBase("select name, key,provinceidname from wjw_check_odp_20231204 "
                                           "where tag = 0 and belong = '{}' and az = '{}'".format(self.ntype, self.az))
        IF_get_flag = True
        all_ips_and_ports = []
        left_ip_num = 0
        proxys_used_stat_dict = {}
        # 查询过的关键词不再次查询
        for row in hos_list:
            name = row[0]
            # key_word保持唯一
            key_word = row[1]
            prov = row[2]
            if key_word is None:
                continue
            if key_word in escape_list or len(key_word) < 4:
                self.logger.info('因为关键词屏蔽，无法查询，当前查询的名称为:' + name)
                continue
            print("{} {} {}".format(self.az, name, key_word))
            if IF_get_flag:
                TimeStame_ip_pool = self.proxymanager.get_usable_ip_pools()
                all_ips_and_ports = []
                left_ip_num = 0
                timestamp = list(TimeStame_ip_pool.keys())[0]
                self.proxymanager.use_ip_pool(timestamp)
                for ip_pool in TimeStame_ip_pool.values():
                    for ip, port in ip_pool.items():
                        all_ips_and_ports.append(ip + ':' + port)
            # left_ip_num 是下标所以要再减1
            if len(all_ips_and_ports) - left_ip_num - 1 == 0:
                IF_get_flag = True
            else:
                IF_get_flag = False
            proxy = all_ips_and_ports[left_ip_num]
            if proxy in proxys_used_stat_dict:
                # 如果代理已存在于字典中，则更新对应的列表
                proxy_list = proxys_used_stat_dict[proxy]
                # 总次数 + 1
                proxy_list[0] += 1
                proxys_used_stat_dict[proxy] = proxy_list
            else:
                # 如果代理不在字典中，则初始化一个新的列表
                proxys_used_stat_dict[proxy] = [1, 0, 0, '生效中']
            left_ip_num = left_ip_num + 1
            self.total_proxy_used += 1  # 增加代理使用计数器
            self.total_calls += 1  # 增加总调用计数器
            o = OrganizationTypeSpider.OrganizationTypeSpider(proxy)
            data_l = o.run(prov, key_word, self.util)
            if data_l == "WRONG":
                self.failure_calls += 1  # 增加失败计数器
                proxy_list = proxys_used_stat_dict[proxy]
                proxy_list[2] = proxy_list[2] + 1
                proxys_used_stat_dict[proxy] = proxy_list.copy()
                continue
            message = o.available_new(data_l)
            if message == "timeout":
                self.failure_calls += 1  # 增加失败计数器
                proxy_list = proxys_used_stat_dict[proxy]
                proxy_list[2] = proxy_list[2] + 1
                proxys_used_stat_dict[proxy] = proxy_list.copy()
                continue
            if message == "未查询到":
                self.success_calls += 1  # 增加成功计数器
                proxy_list = proxys_used_stat_dict[proxy]
                proxy_list[1] = proxy_list[1] + 1
                proxys_used_stat_dict[proxy] = proxy_list.copy()
                self.util.UpdateDataBase("update wjw_check_odp_20231204 set tag = 1 "
                                         "where provinceidname = '{}' and key = '{}'".format(prov, key_word))
            else:
                with open(self.fileName, 'a+', newline='', encoding='utf8') as file:
                    writer = csv.DictWriter(file, fieldnames=self.col_names)
                    for dt in message:
                        writer.writerow(dt)
                self.util.UpdateDataBase("update wjw_check_odp_20231204 set tag = 1 "
                                         "where provinceidname = '{}' and key = '{}'".format(prov, key_word))
                proxy_list = proxys_used_stat_dict[proxy]
                proxy_list[1] = proxy_list[1] + 1
                proxys_used_stat_dict[proxy] = proxy_list.copy()
                self.success_calls += 1  # 增加成功计数器
            remove_list = self.proxymanager.remove_key_from_dict(590)
            for key in remove_list:
                if key in proxys_used_stat_dict.keys():
                    proxy_list = proxys_used_stat_dict[key]
                    proxy_list[3] = '失效'
            self.logger.info(f"Total calls: {self.total_calls}")
            self.logger.info(f"Success calls: {self.success_calls}")
            self.logger.info(f"Failure calls: {self.failure_calls}")
            self.logger.info(f"Proxy used stat: {proxys_used_stat_dict}")
            self.logger.info(f"Total proxy used: {len(proxys_used_stat_dict.keys())}")

    # def run_doc(self):
    #     col = ['name',
    #            'sex',
    #            'approval_company',
    #            'job_type',
    #            'job_address',
    #            'register',
    #            'company',
    #            'range',
    #            'approval',
    #            'ifMore',
    #            'approval_company_',
    #            'effective_',
    #            'back_',
    #            'more',
    #            "beautified",
    #            "doctor_type",
    #            "doctor_pro",
    #            "odp_hos",
    #            ]
    #     escape_list = ['人民医院', '儿童医院', '', '', '']
    #     hos_list = self.util.queryDataBase("select name,key ,province,hospital from {0} "
    #                                        "where belong = '{1}' and az = '{2}' and tag = 0".format('doc_20231225',self.ntype, self.az))
    #     self.if_file_exist(self.fileName, col)
    #     # 查询过的关键词不再次查询
    #     for row in hos_list:
    #         doc_name = row[0]
    #         # key_word保持唯一
    #         key_word = row[1]
    #         prov = row[2]
    #         hos = row[3]
    #         print("{} {} {}".format(self.az, doc_name, key_word))
    #         o = OrganizationTypeSpider.OrganizationTypeSpider()
    #         data_l = o.run_doc(prov, doc_name, key_word, self.util)
    #         num = 0
    #         while data_l == "WRONG":
    #             o = OrganizationTypeSpider.OrganizationTypeSpider()
    #             data_l = o.run_doc(prov, doc_name, key_word, self.util)
    #             num = num + 1
    #             if num == 5:
    #                 break
    #         message = o.available(data_l)
    #         time1 = 0
    #         while message == "ERROR":
    #             o = OrganizationTypeSpider.OrganizationTypeSpider()
    #             data_l = o.run_doc(prov, doc_name, key_word, self.util)
    #             message = o.available(data_l)
    #             time1 = time1 + 1
    #             if time1 == 5:
    #                 break
    #         if message == "受限":
    #             return
    #         if message == "未查询到":
    #             self.util.UpdateDataBase("update {} set tag = 1 "
    #                                      "where name = '{}' and  province = '{}'".format('doc_20231225', doc_name,
    #                                                                                            prov))
    #         if message == "USUAL":
    #             dt_list = o.get_doc(data_l)
    #             with open(self.fileName, 'a+', newline='', encoding='utf8') as file:
    #                 writer = csv.DictWriter(file, fieldnames=col)
    #                 for dt in dt_list:
    #                     dt["odp_hos"] = hos
    #                     writer.writerow(dt)
    #             self.util.UpdateDataBase("update {} set tag = 1 "
    #                                      "where name = '{}' and  province = '{}'".format('doc_20231225', doc_name,
    #                                                                                            prov))

    def if_file_exist(self, file, col):
        if not os.path.isfile(os.getcwd() + '\\' + file):
            with open(file, 'a+', encoding='utf-8') as file:
                writer = csv.writer(file)
                writer.writerow(col)

    def split_keyword(self, _name, prov, city, county):
        key_list = []
        if '诊所' in _name or '卫生室' in _name:
            name = _name.replace(prov, '').replace(city, '').replace(county, '')
            if len(name) > 6:
                key_list.append(name[-6:-1])
            else:
                key_list.append(name)
        if '妇幼保健所' in _name:# or '妇幼保健计划生育服务中心' in _name or '妇幼保健院' in _name
            key_list.append(_name)
            key_list.append(_name.replace('妇幼保健所', '妇幼保健计划生育服务中心'))
            key_list.append(_name.replace('妇幼保健所', '妇幼保健院'))
